1
Immutability and Constants
AI034 Lesson 3
00:00

In a statically typed language, immutability is the foundational state of data, ensuring memory safety and predictable execution. While variables can be shadowed or made mutable, Constants take this a step further by binding a value to a name permanently.

1. The Rigidity of Constants

Unlike standard variables where Type Inference allows the compiler to deduce the data type, constants strictly require an explicit type annotation (e.g., : u32). This maintains rigorous contracts within the code binary.

2. Compile-Time Evaluation

Constants are not merely immutable variables; they are evaluated and "baked" into the program's binary during compile-time. This allows the compiler to perform constant expressions (like 60 * 60 * 3) before the program ever runs, optimizing performance.

let x = 5;Type Inferenceconst SECS: u32 = 60 * 3;Mandatory TypeCompile-Time Math

3. Absolute Immutability

Constants serve as the "single source of truth." They cannot be made mutable with mut and cannot be shadowed in the same scope, ensuring the program's vital parameters remain untouched throughout execution.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>